package com.tg.app.activity.device.ui.cameraview;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.appbase.custom.config.TGConfig;
import com.demo.view.PtzView;
import com.demo.view.TGTabSwitchView;
import com.tange.base.toolkit.DimenUtil;
import com.tange.base.toolkit.StringUtils;
import com.tange.module.device.feature.DeviceFeature;
import com.tg.app.R;
import com.tg.app.activity.device.CameraViewActivity;
import com.tg.app.adapter.PrePositionFragmentAdapter;
import com.tg.app.adapter.TGViewPagerAdapter;
import com.tg.app.camera.AVIOCTRLDEFs;
import com.tg.app.camera.Camera;
import com.tg.app.helper.DeviceFeatureHelper;
import com.tg.app.helper.DeviceHelper;
import com.tg.app.widget.PrePositionControlView;
import com.tg.app.widget.PtzControlView;
import com.tg.appcommon.android.LanguageUtils;
import com.tg.appcommon.android.TGApplicationBase;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceItem;

import java.util.ArrayList;

public class CameraLiveFragment extends CameraLiveViewFragment implements PtzControlView.OnPtzControlTouchListener {
    private static final String TAG = "CameraPlayerView";
    protected PtzView ptzControlView;
    private PrePositionControlView mPrePositionControlView;
    protected View mMicPhoneLayout;

    protected TGTabSwitchView mTabSwitchView;
    protected ViewPager mViewPager;
    protected Camera mCamera;
    protected PtzView.OnPtzControlTouchListener onPtzControlTouchListener;


    public void setPtzControlViewClickable(boolean clickable){
        if (ptzControlView != null){
            ptzControlView.setClickable(clickable);
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        TGApplicationBase.getInstance().removeGlobalObject(PrePositionFragmentAdapter.TAG);
    }

    public void pageClick(int position) {
        TGLog.d("TGTabSwitchView onPageClick position = " + position);
        relMicPhone.setVisibility(position == 0? View.VISIBLE : View.GONE);
    }

    public void pageSelected(int position) {
        TGLog.d("TGTabSwitchView onPageSelected position = " + position);
        relMicPhone.setVisibility(position == 0? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResume() {
        super.onResume();
        CameraViewActivity cameraViewActivity = (CameraViewActivity) getActivity();
        assert cameraViewActivity != null;
        if (cameraViewActivity.isSupportPresetPointNotMultiChannels() && StringUtils.equalsIgnoreCase(TGConfig.APP_ID, TGConfig.APP_ID_HSAB)){
            if (mPrePositionControlView !=  null){
                mPrePositionControlView.init();
                mPrePositionControlView.setSupportPTZReset(cameraViewActivity.isSupportPTZReset());
            }
        }
        hideMic();
    }


    protected final TGTabSwitchView.TabEventListener tabEventListener = new TGTabSwitchView.TabEventListener() {
        @Override
        public void onPageClick(int position) {
            pageClick(position);
        }

        @Override
        public void onPageSelected(int position) {
            pageSelected(position);
        }
    };

    @Override
    protected void setSpeakingShow(){
        super.setSpeakingShow();
        relMicPhone.setVisibility(mTabSwitchView.getCurPageIndex() == 0 && DeviceFeatureHelper.supportSpeaker(deviceFeature)? View.VISIBLE : View.GONE);
    }


    public void setCamera(@NonNull Camera camera) {
        mCamera = camera;
        if (mPrePositionControlView != null){
            mPrePositionControlView.setCamera(camera);
        }
    }

    public void setPtzPos(AVIOCTRLDEFs.Tcis_SetPtzPosReq ptzPos) {
        if (mPrePositionControlView != null){
            mPrePositionControlView.setPtzPos(ptzPos);
        }
    }

    public void receiveIOCtrlData(int type, @NonNull byte[] data) {
        if (mPrePositionControlView != null){
            mPrePositionControlView.receiveIOCtrlData(type, data);
        }
    }

    public static CameraLiveFragment newInstance(DeviceFeature deviceFeature, DeviceItem deviceItem) {
        CameraLiveFragment fragment = new CameraLiveFragment();
        fragment.setArguments(newBundle(deviceFeature, deviceItem));
        return fragment;
    }

    public void setOnPtzControlTouchListener(PtzControlView.OnPtzControlTouchListener onPtzControlTouchListener) {
        this.onPtzControlTouchListener = onPtzControlTouchListener;
    }

    public void createPtzView(PtzView.OnPtzControlTouchListener onPtzControlTouchListener) {
        if (ptzControlView == null || !(ptzControlView instanceof PtzControlView)) {
            ptzControlView = (PtzControlView) View.inflate(getContext(), R.layout.layout_ptz_view, null);
        }

        ptzControlView.setOnPtzControlTouchListener(onPtzControlTouchListener);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        @LayoutRes int resource = R.layout.camera_live_fragment;
        if (!LanguageUtils.isSimChinese(getContext())){
            resource = R.layout.camera_live_fragment_chinese;
        }
        View view = inflater.inflate(resource, container, false);
        layout = view.findViewById(R.id.cameraview);

        mTabSwitchView = view.findViewById(R.id.switchTab);
        mViewPager = view.findViewById(R.id.viewPager);

        createPtzView(this);
        mTabSwitchView.setTabEventListener(tabEventListener);

        setPagerAdapter();
        mTabSwitchView.setViewPager(mViewPager);


        this.relMicPhone = view.findViewById(R.id.rel_camera_live_micphone);

        btnSpeaking = view.findViewById(R.id.btn_camera_live_speaking);
        setBtnSpeakingOnTouchListener();
        recordViewIcon = view.findViewById(R.id.record_view_icon_inside);
        recordViewShadowLayout = view.findViewById(R.id.yc_shadow_layout);
        btnSpeakingLand = view.findViewById(R.id.btn_camera_live_speaking_land);
        btnSpeaking.setRecordAudioListener(this);
        btnSpeakingLand.setRecordAudioListener(this);

        lightView = view.findViewById(R.id.camera_live_light_view);
        mMicPhoneLayout = view.findViewById(R.id.camera_live_speaking_land_layout);
        mPlayerResolutionLayout = view.findViewById(R.id.camera_live_player_resolution_land_layout);
        setClicked(false);
        lightView.setStyle(Paint.Style.FILL);
        lightView.setColor(getContext().getResources().getColor(R.color.breathing_light_color));
        lightView.setParentView(btnSpeaking);
        return view;
    }

    @Override
    public void onRecordStop() {
        super.onRecordStop();
        if (recordViewIcon != null) {
            recordViewIcon.setImageResource(R.drawable.ic_tange_global_icon_microphone_gray);
        }
        if (recordViewShadowLayout != null) {
            recordViewShadowLayout.setShadowColor(getResources().getColor(R.color.white_rounded_btn_shadow_color));
        }
    }


    public void setPagerAdapter() {
        TGLog.d("setPagerAdapter");
        ArrayList<View> list = new ArrayList<>();
        list.add(ptzControlView);
        CameraViewActivity cameraViewActivity = (CameraViewActivity) getActivity();
        if (cameraViewActivity == null) return;
        if (cameraViewActivity.isSupportCruise()){
            RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams)mTabSwitchView.getLayoutParams();
            if (layoutParams != null){
                layoutParams.width = DimenUtil.dp2px(getContext(), 250);
                mTabSwitchView.setLayoutParams(layoutParams);
                mTabSwitchView.setVisibility(View.VISIBLE);
                mTabSwitchView.setText(getString(R.string.txt_cruise_and_presetpos), getContext());
            }

            mPrePositionControlView = (PrePositionControlView)View.inflate(getContext(), R.layout.layout_prepostion_view_cruise, null);
            list.add(mPrePositionControlView);
            if (mCamera != null){
                mPrePositionControlView.setCamera(mCamera);
            }
            mPrePositionControlView.setSupportWatchPos(cameraViewActivity.isSupportWatchPos());

        }else{
            if (cameraViewActivity.isSupportPresetPointNotMultiChannels() && StringUtils.equalsIgnoreCase(TGConfig.APP_ID, TGConfig.APP_ID_HSAB) && !isLandscape){
                TGLog.d("setPagerAdapter true");
                mTabSwitchView.setVisibility(View.VISIBLE);
                showPtzView(View.VISIBLE);
                mPrePositionControlView = (PrePositionControlView)View.inflate(getContext(), R.layout.layout_prepostion_view, null);

                list.add(mPrePositionControlView);
                if (mCamera != null){
                    mPrePositionControlView.setCamera(mCamera);
                }
                mPrePositionControlView.setSupportWatchPos(cameraViewActivity.isSupportWatchPos());
            }else {
                mTabSwitchView.setVisibility(View.GONE);
            }

        }

        if (isLandscape){
            mTabSwitchView.setVisibility(View.GONE);
        }

        mViewPager.setAdapter(new TGViewPagerAdapter(list));
    }

    public void adjustInfoView(boolean misPhoneShow, boolean controlViewShow, boolean resolutionShow) {

        super.adjustInfoView(misPhoneShow, resolutionShow, controlViewShow);

        boolean showMic = misPhoneShow;
        if (!DeviceFeatureHelper.supportSpeaker(deviceFeature)){
            showMic = false;
        }

        if (isLandscape) {
            mMicPhoneLayout.setVisibility(showMic ? View.VISIBLE : View.GONE);
            showPtzView(controlViewShow ? View.VISIBLE : View.GONE);
            mPlayerResolutionLayout.setVisibility(resolutionShow ? View.VISIBLE : View.GONE);
        }
        mTabSwitchView.setVisibility(isLandscape? View.GONE : View.VISIBLE);
        setPagerAdapter();
    }


    @Override
    protected void configurationView(int orientation) {
        super.configurationView(orientation);
        TGLog.d("configurationView" + DeviceFeatureHelper.supportPTZ(deviceFeature));
        TGLog.trace();
        if (ptzControlView != null){
            ptzControlView.setLandscape(isLandscape);
        }

        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            if (ptzControlView != null){
                ptzControlView.setImageView(R.mipmap.ptz_control_bg_land);
            }

            if (DeviceFeatureHelper.supportPTZ(deviceFeature)) {
                mViewPager.setVisibility(View.VISIBLE);
                if (ptzControlView != null){
                    ptzControlView.setVisibility(View.VISIBLE);
                }

                adjustInfoView(false, true, false);
            }
        } else {
            if (ptzControlView != null){
                ptzControlView.setImageView(R.mipmap.ic_tange_switch_ptz);
            }

            setSpeakingShow();
        }
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        TGLog.i(TAG, "Orientation");

    }


    public void updateDeviceFeature(DeviceFeature deviceFeature) {
        super.updateDeviceFeature(deviceFeature);
        TGLog.i(TAG, "updateDeviceFeature " + DeviceFeatureHelper.supportPTZ(deviceFeature) + ", " + DeviceFeatureHelper.supportMultiChannels(deviceFeature));
        TGLog.trace();
        if (DeviceFeatureHelper.supportPTZ(deviceFeature)) {
            createPtzView(this);
            showPtzView(View.VISIBLE);
            if (DeviceFeatureHelper.supportMultiChannels(deviceFeature)){
                showPtzView(View.GONE);
            }
        } else {
            mViewPager.setVisibility(View.GONE);
            if (ptzControlView != null){
                ptzControlView.setVisibility(View.GONE);
            }

        }
        setSpeakingShow();
        setPagerAdapter();
    }

    public int getPtzControlViewVisible() {
        return ptzControlView == null? View.GONE : ptzControlView.getVisibility();
    }

    @Override
    public void setPtzControlViewVisible(boolean show) {
        if (ptzControlView != null){
            TGLog.d("setPtzControlViewVisible show = " + show);
            showPtzView(show ? View.VISIBLE : View.GONE);
        }
    }

    protected void showPtzView(int i) {
        if (ptzControlView != null){
            ptzControlView.setVisibility(i);
        }

        mViewPager.setVisibility(i);
    }

    public void setClicked(boolean enable) {
        CameraViewActivity cameraViewActivity = (CameraViewActivity) getActivity();
        if (!DeviceHelper.isOnline(mDeviceItem) && cameraViewActivity != null && !cameraViewActivity.isLocalConnect()) {
            super.setClicked(false);
            enable = false;
        } else {
            super.setClicked(enable);
        }

        TGLog.d(TAG, "enable2 = " + enable);
        if (ptzControlView != null){
            ptzControlView.setClickable(enable);
            if (enable) {
                ptzControlView.setImageView(R.mipmap.ic_tange_switch_ptz);
            } else {
                ptzControlView.setImageView(R.mipmap.ptz_control_bg_disable_new);
            }
        }
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        updateDeviceFeature(deviceFeature);
        TGLog.i(TAG, "onActivityCreated");
    }


    public void setSendPTZCmd(boolean isSend) {
        ptzControlView.setSendPTZCmd(isSend);
    }

    @Override
    public void onShortCmd(int control) {
        if (onPtzControlTouchListener != null) {
            onPtzControlTouchListener.onShortCmd(control);
        }
    }

    @Override
    public void onStopCmd() {
        if (onPtzControlTouchListener != null) {
            onPtzControlTouchListener.onStopCmd();
        }
    }

    @Override
    public void onLongCmd(int control) {
        if (onPtzControlTouchListener != null) {
            onPtzControlTouchListener.onLongCmd(control);
        }
    }

}

